/**
 * author: MaHX
 * date: 2019/8/24/20:12
 * description: 八皇后算法(递归)
 **/
public class EightQueen {
    static int[][] map = new int[8][8];
    static int count = 1;
    public static void show(){
        System.out.println("第"+count+"种解法");
        for (int i = 0; i<8; i++) {
            for (int j = 0; j < 8; j++) {
                System.out.print(map[i][j]+"  ");
            }
            System.out.println();
        }
        count++;
    }
    public static Boolean check(int row,int col){
       //前方
        for (int x = row-1; x >=0 ; x--) {
           if ( map[x][col] == 1) return false;
        }
        //左斜
        for (int x = row-1,y=col-1;x>=0&&y>=0;x--,y--){
            if (map[x][y] == 1)return false;
        }
        //右斜
        for (int x= row-1,y=col+1;x>=0&&y<8;x--,y++){
            if (map[x][y] == 1)return false;
        }
        return true;
    }
    public static void play(int row){
        for (int i = 0; i < 8; i++) {
            if (check(row,i)){
                map[row][i] = 1;
                if (row==7){
                    show();
                    map[row][i] = 0;
                }else {
                    play(row+1);
                    //当调用循环结束时，去掉上一层的皇后，接着之前没跑的循环
                    map[row][i] = 0;
                }
            }
        }

    }

    public static void main(String args[]){
     play(0);
    }
}
